package com.cloudcc.mobile.view.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import butterknife.Bind;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.header.DefaultMaterialHeader;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.header.MaterialHeader;
import com.cloudcc.cloudframe.util.DensityUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.ExaminationApprovalAdapter;
import com.cloudcc.mobile.dao.impl.ApprovalEngineImpl;
import com.cloudcc.mobile.entity.approval.ApprovalModel;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.google.gson.JsonObject;
/**
 * 审批
 * @author lhy
 *
 */
public class ExaminationAndApprovalOfHistoryActivty extends BaseActivity implements PtrHandler,OnClickListener, ResultCallBack<List<ApprovalModel>>{
	private ExaminationApprovalAdapter adapter;
	private ArrayList<ApprovalModel> approvallist;
	private ApprovalEngineImpl engine = new ApprovalEngineImpl();
	private String relatedId;//布局id
	@Bind(R.id.headerbar)
	CloudCCTitleBar headerbar;
	@Bind(R.id.left_image)
	ImageView backbtn;
	@Bind(R.id.listview)
	ListView mListView;
	@Bind(R.id.reback_request)
	LinearLayout reback;
	@Bind(R.id.again_allocation)
	LinearLayout allocation;
	@Bind(R.id.going_approval)
	LinearLayout approval;
	@Bind(R.id.load_more_list_view_ptr_frame)
	PtrFrameLayout mRefreshLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		backbtn.setOnClickListener(this);
		initHeader();
		initadapter();
		initRefresh();
	}
	private void initHeader() {
		Intent intent = getIntent();
		relatedId = intent.getStringExtra("relatedId");
		relatedId = "001201599DA06396SE7p";
		headerbar.setTitle("审批历史");
	}
	private void initadapter(){
		adapter = new ExaminationApprovalAdapter(this);
		mListView.setAdapter(adapter);
	}
	protected void initRefresh() {
		mRefreshLayout.setLoadingMinTime(1000);
		mRefreshLayout.setPtrHandler(this);

		final MaterialHeader header = new DefaultMaterialHeader(this);
		int[] colors = getResources().getIntArray(R.array.google_colors);
		header.setColorSchemeColors(colors);
		header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
		header.setPadding(0, DensityUtils.dpToPixel(mContext, 10), 0,
				DensityUtils.dpToPixel(mContext, 5));
		header.setPtrFrameLayout(mRefreshLayout);
		mRefreshLayout.setHeaderView(header);
		mRefreshLayout.addPtrUIHandler(header);
		mRefreshLayout.setResistance(5.0f);
	}
	private void refreshData() {
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("relatedId", relatedId);
		headerbar.beginProgress();
		engine.getApprovalHistoryList(jsonObject,this);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.examination_approval;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_image:
			ExaminationAndApprovalOfHistoryActivty.this.finish();
			break;
		case R.id.reback_request:
		
			break;
		case R.id.again_allocation:
			
			break;
		case R.id.going_approval:
			
			break;
		default:
			break;
		}
	}
	@Override
	public boolean checkCanDoRefresh(PtrFrameLayout frame, View content,
			View header) {
		return PtrDefaultHandler.checkContentCanBePulledDown(frame, mListView,
				header);
	}
	@Override
	public void onRefreshBegin(PtrFrameLayout frame) {
		// TODO Auto-generated method stub
		refreshData();
	}
	@Override
	public void onSuccess(List<ApprovalModel> t) {
		headerbar.completeProgress();
		mRefreshLayout.refreshComplete();
		for (ApprovalModel approvalModel : t) {
			approvallist.add(approvalModel);
		}
		adapter.changeData(approvallist);
		
	}
	@Override
	public void onFailure(ErrorInfo error) {
		headerbar.completeProgress();
		Toast.makeText(ExaminationAndApprovalOfHistoryActivty.this, "获取数据失败！", 
				Toast.LENGTH_LONG).show();
		for(int i=0;i<10;i++){
			ApprovalModel model = new ApprovalModel("已提交审批", "2015/11/19 15:22", "已提交", "王刚", "郑艳", 
					"无", "无", "");
			approvallist.add(model);
		}
		adapter.changeData(approvallist);
	}
	

}
